package logic

import (
	"context"
	"shell/models/mysql"

	"shell/houserpc/houserpc"
	"shell/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PreListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPreListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PreListLogic {
	return &PreListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PreListLogic) PreList(in *houserpc.PreListRequest) (*houserpc.PreListResponse, error) {
	// todo: add your logic here and delete this line
	var pre []mysql.PreUser

	yk := mysql.DB.Debug().Model(&mysql.Pre{}).
		Select("pres.*,users.mobile as user_mobile,users.name as user_name,houses.address as house_address,houses.area as house_area").
		Joins("left join users on users.id=pres.user_id ").
		Joins("left join houses on houses.id=pres.house_id")
	if in.PreTimeOrder == 1 {
		yk = yk.Order("pre_time desc")
	}

	if in.PreTimeOrder == 2 {
		yk = yk.Order("pre_time asc")
	}

	yk.Find(&pre)

	var preList []*houserpc.PreList
	for _, m := range pre {
		list := houserpc.PreList{
			UserId:       m.UserId,
			HouseId:      m.HouseId,
			PreTime:      m.PreTime,
			Status:       m.Status,
			PreId:        int64(m.ID),
			UserMobile:   m.UserMobile,
			UserName:     m.UserName,
			HouseTitle:   m.HouseTitle,
			HouseAddress: m.HouseAddress,
			HouseArea:    m.HouseArea,
		}
		preList = append(preList, &list)
	}
	return &houserpc.PreListResponse{
		List: preList,
	}, nil
}
